package tool

import (
	"game_server/framework/global"
	"github.com/duke-git/lancet/v2/netutil"
	"time"

	"github.com/duke-git/lancet/v2/cryptor"
	"github.com/duke-git/lancet/v2/random"

	"github.com/bwmarrin/snowflake"
)

func Snowflake(nodeId int64) int64 {
	node, _ := snowflake.NewNode(nodeId)
	id := node.Generate()
	return id.Int64()
}

func CurrentTimestamp() int64 {
	return time.Now().UnixMilli()
}

func CurrentDateTime() time.Time {
	return time.Now()
}

func EncryptSHA256(content string) string {
	return cryptor.Sha256(content)
}

func GenerateSalt10() string {
	return random.RandString(10)
}

func GenerateRandomIntScope(start int, end int) int {
	return random.RandInt(start, end)
}

type HallServerHttpRes struct {
	Code string      `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func HttpSendFunction(req *netutil.HttpRequest) (*HallServerHttpRes, error) {
	httpClient := netutil.NewHttpClient()
	resp, err := httpClient.SendRequest(req)
	if err != nil {
		global.Logger.Errorf("err:%v", err)
		return nil, err
	}
	var hallServerHttpRes HallServerHttpRes
	err = httpClient.DecodeResponse(resp, &hallServerHttpRes)
	if err != nil {
		global.Logger.Errorf("err:%v", err)
		return nil, err
	}
	return &hallServerHttpRes, nil
}
